#######################################################################################################
# Autor: Jan Stuckatz
# Date: 2019/03/04
# Paper Title: Political Alignment between Firms and Employees in the United States: Evidence from a new Dataset
# Replication of Result Tables in the Main Text and Appendix
######################################################################################################


# set wd to where code is, if using RStudio
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

# start log file
sink("log_main.txt")



# Load Necessary Packages
library(dplyr)
library(rio)
library(tidyr)
library(lfe)
library(stargazer)
library(xtable)



#########################################
########### Main Text Figures ###########
########################################

#### Figure 2
load("R_shares.RDta") # read data
ic_RS <- ic_RS %>% filter(office_race == "all")

# plot
pdf("figure_2.pdf", width = 10, height = 8) # open graphics device
par(mfrow = c(1,1), mar = c(5,5.5,4,2), las = 1, cex.axis = 1.35, cex.lab = 1.6, cex.main = 1.8)
plot(ic_RS$Rshare_c, ic_RS$Rshare_i, pch = 16,col = "darkgrey", las = 0.8, font.lab = 2, axes=FALSE,
     main = "",  ylim = c(0,1), xlim = c(0,1),
     xlab = "Firm REP Contribution Share", ylab = "Employee REP Contribution Share")
grid(col = "lightgrey", lty = "solid")
points(ic_RS$Rshare_c, ic_RS$Rshare_i, pch = 1,col = "black", las = 0.8, font.lab = 2,
       main = "",  ylim = c(0,1), xlim = c(0,1))
text(x = 0.1, y = 0.95, labels = expression(paste(beta, " = 0.61")), cex = 1.75) # R2 = 0.195
axis(1, at = seq(0, 1, 0.2))
axis(2, at = seq(0, 1, 0.2))
abline(lm(ic_RS$Rshare_i ~ ic_RS$Rshare_c), col = "black", lwd = 2)
dev.off() # close graphics device
rm(ic_RS) # clean up


#### Figure 3, Histogram of Dependent Variable
# load data
load("ind_dv.RDta")

pdf("figure_3.pdf", width = 8, height = 7)
par(mfcol=c(1,1), mar = c(5.5,5.5,1,1.5), cex.lab = 1.75, cex.axis = 1.35)
hist(ind_dv$Align_con, 50, col = "darkgrey", main = "", ylab = "",
     xlab = "", las = 1,  font.lab = 2)
grid(col = "lightgrey", lty = "solid")
hist(ind_dv$Align_con, 50, col = "darkgrey",
     xlab = "", las = 1,  font.lab = 2, add = TRUE)
title(ylab = "Frequency", xlab = "Partisan Alignment", font.lab = 2, line = 4)
dev.off()
rm(ind_dv) # clean up



##### Figure 4
# Figure 4a, Alignment vs. Employees
load("align_firmsize.RDta") # read data

pdf("figure_4a.pdf", width = 8, height = 8)
par(mfrow = c(1,1), mar = c(5,5.5,2,1), las = 1, cex.axis = 1.35, cex.lab = 1.6, cex.main = 2)
plot(pc_agg$lemp, pc_agg$Align, pch = 16,col = "darkgrey", las = 0.8, font.lab = 1, axes=FALSE,
     main = "",  ylim = c(0.2,1),
     xlab = "Log(Employees)", ylab = "Mean Firm Alignment")
grid(col = "lightgrey", lty = "solid")
points(pc_agg$lemp, pc_agg$Align, pch = 1,col = "black", las = 0.8, font.lab = 2,
       main = "",  ylim = c(0.2,1))
text(x = 1, y = 0.975, labels = expression(paste(beta, " = -0.02" ,3)), cex = 1.75)
axis(1, at = seq(0, 9, 2))
axis(2, at = seq(0.2, 1, 0.2))
abline(lm(pc_agg$Align ~ pc_agg$lemp), col = "black", lwd = 2)
dev.off()
rm(pc_agg) # clean up


# Figure 4b, Alignment vs. CEO or not
load("align_ceo.RDta")

pdf("figure_4b.pdf", width = 7, height = 8)
par(mfrow = c(1,1), mar = c(5,5.5,2,1), las = 1, cex.axis = 1.35, cex.lab = 1.6, cex.main = 1.8)

boxplot(Align ~ CEO, data = pac_ceo, horizontal = FALSE, axes=FALSE,  # be careful, order of categories is reversed when TRUE
        las = 1, outpch=21, outbg = "darkgrey", outcex = 0.65, col = "grey55", boxwex = 0.25, lwd = 1.35,
        ylim = c(0, 1), xlim = c(0.75, 2.25), xaxt = "n", ylab = "", xlab = "")
title(ylab = "Alignment (firm mean)", xlab = "Occupation", font.lab = 2)
abline(h = 0.5, lwd = 2, lty = "dashed", col = "lightgrey")
grid(col = "lightgrey", lty = "solid")
boxplot(Align ~ CEO, data = pac_ceo, horizontal = FALSE, axes=FALSE, # be careful, order of categories is reversed when TRUE
        las = 1, outpch=21, outbg = "darkgrey", outcex = 0.65, col = "grey55", add = TRUE, boxwex = 0.25, lwd = 1.35,
        ylim = c(0, 1), xlim = c(0.75, 2.25),xaxt = "n")
axis(1, at = c(1,2), labels = c("Other Employees", "Chief Executives"))
axis(2, at = seq(0, 1, 0.2))

dev.off()
rm(pac_ceo) # clean up









#############################################
########### Main Text Regressions ###########
#############################################

# Load Individual-Level Data
load("main_results_ind.RDta")
load("main_results_firm.RDta")


# Table 4
lm_RS1 <- felm(Rshare_i ~ Rshare_c  | 0 | 0 | gvkey, data = ic_RS)
lm_RS2 <- felm(Rshare_i ~ Rshare_c + emp + capx + ppent + cogs + sale | 0 | 0 | gvkey, data = ic_RS)
lm_RS3 <- felm(Rshare_i ~ Rshare_c + emp + capx + ppent + cogs + sale | cycle | 0 | gvkey, data = ic_RS)
lm_RS4 <- felm(Rshare_i ~ Rshare_c + emp + capx + ppent + cogs + sale | cycle + gvkey  | 0 | gvkey, data = ic_RS)


stargazer::stargazer(lm_RS1, lm_RS2, lm_RS3, lm_RS4, type = "latex", title = "Regression Results: Employee and Firm Donations", label = "lm_RS1",
                     out = "table_4.tex",
                     covariate.labels = c("Firm REP Donation Share"), dep.var.labels = rep(c("Employee REP Donation Share"),1), omit = c("emp", "capx", "ppent", "cogs", "sale", "Constant"),
                     add.lines = list(c("Firm Controls",  "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                                      c("Cycle FEs",  "", "", "$\\checkmark$", "$\\checkmark$"),
                                      c("Firm FEs",  "", "", "","$\\checkmark$")),omit.stat = c("rsq", "ser"),
                     column.sep.width = "3pt", notes = "Standard errors clustered by firm.", notes.align = "l")

rm(lm_RS1, lm_RS2, lm_RS3, lm_RS4) # clean up





# Table 5
lm_emp1 <- felm(Align ~  emp | 0   | 0 | gvkey, data = pc_don)
lm_emp2 <- felm(Align ~  emp + capx + ppent + cogs + sale | 0  | 0 | gvkey, data = pc_don)
lm_emp3 <- felm(Align ~  emp + capx + ppent + cogs + sale | cycle  | 0 | gvkey, data = pc_don)
lm_emp4 <- felm(Align ~  emp + capx + ppent + cogs + sale | cycle + soc_2010  | 0 | gvkey, data = pc_don)
lm_emp5 <- felm(Align ~  emp + capx + ppent + cogs + sale | cycle + soc_2010 + fips  | 0 | gvkey, data = pc_don)
lm_emp6 <- felm(Align ~  emp + capx + ppent + cogs + sale | cycle + soc_2010 + fips + naics2  | 0 | gvkey, data = pc_don)


stargazer::stargazer(lm_emp1, lm_emp2, lm_emp3, lm_emp4, lm_emp5, lm_emp6, type = "latex", title = "Regression Results: Firm Size and Alignment", label = "lm_emp1",
                     out = "table_5.tex",
                     covariate.labels = c("log(Employees)"), dep.var.labels = rep(c("Align"),1), omit = c("capx", "ppent", "cogs", "sale", "Constant"),
                     add.lines = list(c("Firm Controls",  "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                                      c("Cycle FEs",  "", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                                      c("Occupation FEs",  "", "", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                                      c("County FEs", "", "", "", "", "$\\checkmark$", "$\\checkmark$"),
                                      c("NAICS 2-digit FEs", "", "", "", "", "", "$\\checkmark$")),omit.stat = c("rsq", "ser"),
                     column.sep.width = "3pt", notes = "Standard errors clustered by firm.", notes.align = "l")

rm(lm_emp1, lm_emp2, lm_emp3, lm_emp4, lm_emp5, lm_emp6)



# Table 6
lm_ceo1 <- felm(Align ~  CEO  | 0 | 0 | gvkey, data = pc_don)
lm_ceo2 <- felm(Align ~  CEO + emp + capx + ppent + cogs + sale | 0 | 0 | gvkey, data = pc_don)
lm_ceo3 <- felm(Align ~  CEO + emp + capx + ppent + cogs + sale | cycle  | 0 | gvkey, data = pc_don)
lm_ceo4 <- felm(Align ~  CEO + emp + capx + ppent + cogs + sale | cycle + gvkey  | 0 | gvkey, data = pc_don)
lm_ceo5 <- felm(Align ~  CEO + emp + capx + ppent + cogs + sale | cycle + gvkey + fips | 0 | gvkey, data = pc_don)
lm_ceo6 <- felm(Align ~  CEO + emp + capx + ppent + cogs + sale | cycle + gvkey + fips + soc2 | 0 | gvkey, data = pc_don)


stargazer::stargazer(lm_ceo1, lm_ceo2, lm_ceo3, lm_ceo4, lm_ceo5, lm_ceo6, type = "latex", title = "Regression Results: Chief Executives and Alignment", label = "lm_ceo1",
                     out = "table_6.tex",
                     covariate.labels = c("CEO"), dep.var.labels = rep(c("Align"),1), omit = c("emp", "capx", "ppent", "cogs", "sale", "Constant"),
                     add.lines = list(c("Firm Controls",  "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                                      c("Cycle FEs",  "", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                                      c("Firm FEs",  "", "", "","$\\checkmark$", "$\\checkmark$",  "$\\checkmark$"),
                                      c("County FEs", "", "", "", "", "$\\checkmark$", "$\\checkmark$"),
                                      c("SOC 2-digit FEs",  "", "", "", "", "", "$\\checkmark$")),omit.stat = c("rsq", "ser"),
                     column.sep.width = "3pt", notes = "Standard errors clustered by firm.", notes.align = "l")

rm(lm_ceo1, lm_ceo2, lm_ceo3, lm_ceo4, lm_ceo5, lm_ceo6)
rm(ic_RS, pc_don)





############################
############################
############################


# close log file
sink()
